﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BDShop.Entities;
using BDShop.DAL;
using IsolationLevel = System.Transactions.IsolationLevel;
using System.Data;

namespace BDShop.Business
{
    public class UserService
    {
        public static User GetByUserName(string userName)
        {
            User user = new User();
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                user = context.Users.Where(u => u.UserName == userName).FirstOrDefault();
            }

            return user;
        }

        public static User GetByUserID(int userID)
        {
            User user = new User();
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                user = context.Users.Where(u => u.ID == userID).FirstOrDefault();
            }

            return user;
        }

        public static User GetByEmail(string email)
        {
            User user = new User();
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                user = context.Users.Where(u => u.Email == email).FirstOrDefault();
            }

            return user;
        }

        public static User GetByPhone(string phone)
        {
            User user = new User();
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                user = context.Users.Where(u => u.Phone == phone).FirstOrDefault();
            }

            return user;
        }

        public static bool Update(User userModel)
        {
            try
            {
                User user = new User();
                using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
                {
                    user = context.Users.Where(u => u.ID == userModel.ID).FirstOrDefault();

                    user.FullName = userModel.FullName;
                    user.Address = userModel.Address;
                    user.Birthday = userModel.Birthday;
                    user.Gender = userModel.Gender;
                    user.Phone = userModel.Phone;

                    context.Entry(user).State = EntityState.Modified;
                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }
    }
}
